package com.ruoyi.lifecycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lifecycle.domain.bo.TDistributorBo;
import com.ruoyi.lifecycle.domain.vo.TDistributorVo;
import com.ruoyi.lifecycle.domain.TDistributor;
import com.ruoyi.lifecycle.mapper.TDistributorMapper;
import com.ruoyi.lifecycle.service.ITDistributorService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 销售商信息Service业务层处理
 *
 * @author chq
 * @date 2025-06-06
 */
@RequiredArgsConstructor
@Service
public class TDistributorServiceImpl implements ITDistributorService {

    private final TDistributorMapper baseMapper;

    /**
     * 查询销售商信息
     */
    @Override
    public TDistributorVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询销售商信息列表
     */
    @Override
    public TableDataInfo<TDistributorVo> queryPageList(TDistributorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDistributor> lqw = buildQueryWrapper(bo);
        Page<TDistributorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询销售商信息列表
     */
    @Override
    public List<TDistributorVo> queryList(TDistributorBo bo) {
        LambdaQueryWrapper<TDistributor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TDistributor> buildQueryWrapper(TDistributorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDistributor> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), TDistributor::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TDistributor::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getFullname()), TDistributor::getFullname, bo.getFullname());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TDistributor::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), TDistributor::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getContactmobile()), TDistributor::getContactmobile, bo.getContactmobile());
        lqw.eq(StringUtils.isNotBlank(bo.getWeb()), TDistributor::getWeb, bo.getWeb());
        lqw.eq(StringUtils.isNotBlank(bo.getHotline()), TDistributor::getHotline, bo.getHotline());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TDistributor::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增销售商信息
     */
    @Override
    public Boolean insertByBo(TDistributorBo bo) {
        TDistributor add = BeanUtil.toBean(bo, TDistributor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改销售商信息
     */
    @Override
    public Boolean updateByBo(TDistributorBo bo) {
        TDistributor update = BeanUtil.toBean(bo, TDistributor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TDistributor entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除销售商信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
